<?php 

/**
 * Copyright (c) 2012 Jan Kockrow
 * Original idea for the .NET AutoMapper Copyright (c) 2010 Jimmy Bogard (http://automapper.org/)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

interface IValueResolver {
    function Resolve($source);
}

interface ITypeConverter {
    function Convert($source);
}

class Property {
    /** @var string */
    public $Name;
    /** @var string */
    public $Class;
    
    /**
     * Creates a new Property instance frtom a fully specified property name.
     * Example: 'FullNameSpace\ClassName::PropertyName'
     *
     * @param type $fullClassAndPropertyName 
     */
    public function __construct($fullClassAndPropertyName) {
        $propertyValues = \explode('::', $fullClassAndPropertyName);
        $this->Class = $propertyValues[0];
        $this->Name = $propertyValues[1];
    }
}

class Map {
    /** @var Adminomatic\AutoMapper\Property */
    public $ForMember;
    /** @var Adminomatic\AutoMapper\Property */
    public $FromMember;
    /** @var Adminomatic\AutoMapper\ITypeConverter */
    public $TypeConverter;
    /** @var Adminomatic\AutoMapper\IValueResolver */
    public $ValueResolver;
    
    public function __construct($forMember, $fromMember=null) {
        $this->ForMember = new Property($forMember);
        if($fromMember !== null) {
            $this->FromMember = new Property($fromMember);
        }
    }

    /**
     * Adds the given fully qualified property name as source to this Map.
     * Example: 'FullNameSpace\ClassName::PropertyName'
     *
     * @param string $source
     * @return Adminomatic\AutoMapper\MappingResult 
     */
    public function FromMember($source) {
        $this->FromMember = new Property($source);
        return new MappingResult($this);
    }
    
    /**
     * Specifies an IValueResolver for this Map.
     *
     * @param IValueResolver $resolver
     * @return Adminomatic\AutoMapper\ResolutionResult 
     */
    public function ResolveUsing(IValueResolver $resolver) {
        $this->ValueResolver = $resolver;
        return new ResolutionResult($this);
    }
}

class Mapper {
    
    private $maps = array();

    /**
     * Creates a map for the specified properties.
     * Example: 'FullNameSpace\ClassName::PropertyName'
     *
     * @param string $forMember
     * @param string $fromMember 
     */
    public function CreateMap($forMember, $fromMember)  {
        $this->maps[$forMember] = new Map($forMember, $fromMember);
    }
    
    /**
     * Creates a map for the specified properties using the specified ITypeConverter.
     * Example: 'FullNameSpace\ClassName::PropertyName'
     *
     * @param string $forMember
     * @param string $fromMember
     * @param ITypeConverter $typeConverter 
     */
    public function CreateMapUsingConverter($forMember, $fromMember, ITypeConverter $typeConverter) {
        $this->maps[$forMember] = new Map($forMember, $fromMember);
        $this->maps[$forMember]->TypeConverter = $typeConverter;
    }
    
    /**
     * Creates a map for the specified property using the specified IValueResolver.
     * Example: 'FullNameSpace\ClassName::PropertyName'
     *
     * @param string $forMember
     * @param IValueResolver $valueResolver 
     */
    public function CreateMapUsingResolver($forMember, IValueResolver $valueResolver) {
        $this->maps[$forMember] = new Map($forMember);
        $this->maps[$forMember]->ValueResolver = $valueResolver;
    }
    
    /**
     * Copies the values of all public properties of $source to the corresponding public properties of $destination 
     * with the same name if existing and returns the destination object.
     *
     * @param object $destination
     * @param object $source
     * @return object
     */
    public function Map($destination, $source) {
        if($source === null) {
            return null;
        }
                    
        if(\is_object($destination)) {
            if(\get_class($destination) == '\ReflectionProperty') {
                return $this->MapProperty($destination, $source);
            }
            return $this->MapObject($destination, $source);
        }
        
        if(\is_array($destination)) {
            return $this->MapScalarArray($destination, $source);
        }
        
        if(\class_exists($destination)) {
            return $this->MapObject(new $destination, $source);
        }
        
        //$destination type is unknown and can't be resolved. We assume it is a scalar then...
        if(\is_scalar($source)) {
            return $source;
        }

        return null;
    }
    
    private function MapMapping(Map $map, \ReflectionProperty $destinationProperty, $source){
        if($map->FromMember !== null) {
            $sourceValue = $source->{$map->FromMember->Name};
            if($map->TypeConverter !== null) {
                return $map->TypeConverter->Convert($sourceValue);
            }
            return $this->MapProperty($destinationProperty, $sourceValue);

        }
        if($map->ValueResolver !== null) {
            return $map->ValueResolver->Resolve($source);
        }
    }
    
    private function MapObject($destination, $source) {
        if(!\is_object($source)) {
            return null;
        }
        
        if(\get_class($destination) == \get_class($source)) {
            return $source;
        }

        $destinationReflection = new \ReflectionClass($destination);
        foreach($destinationReflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $destinationProperty) {
            
            $map = null;
            if($this->TryGetMap($destinationProperty, $map)) {
                $destination->{$destinationProperty->name} = $this->MapMapping($map, $destinationProperty, $source);
                continue;
            }

            $srcProperty = null;
            if(self::TryGetReflectionProperty($destinationProperty->name, $source, $srcProperty)) {
                $destination->{$destinationProperty->name} = $this->MapProperty($destinationProperty, $srcProperty->getValue($source));
                continue;
            }
        }
        return $destination;
    }
    
    private function MapObjectArray($destinationType, $source) {
        if(!\is_array($source)) {
            //TODO: Check for ITypeConverter
            return array();
        }
        
        $result = array();
        foreach($source as $element) {
            $result[] = $this->Map(new $destinationType, $element);
        }
        return $result;
    }
    
    private function MapProperty(\ReflectionProperty $destinationProperty, $source) {
        $destinationType = null;
        if(self::TryParseType($destinationProperty, $destinationType)) {
            if(\class_exists($destinationType)) {
                return $this->MapObject(new $destinationType, $source);
            }
            
            if(self::IsScalarArrayType($destinationType)) {
                return $this->MapScalarArray(array(), $source);
            }
            
            if(self::IsScalarType($destinationType)) {
                return $this->Map(0, $source);
            }
            
            $arrayType = null;
            if(self::TryGetObjectArrayClass($destinationType, $arrayType)) {
                if(\class_exists($arrayType)) {
                    return $this->MapObjectArray($arrayType, $source);
                }
                if(self::IsScalarType($arrayType)) {
                    return $this->MapScalarArray(array(), $source);
                }
            }
        }
        return null;
    }
    
    private function MapScalarArray(array $destination, $source) {
        if(!\is_array($source)) {
            return array();
        }
        
        $result = array();
        foreach($source as $key => $value) {
            $result[$key] = $value;
        }
        return $result;
    }

    private static function IsScalarArrayType($type) {
        if(\strtolower($type) == 'array') {
            return true;
        }
        return false;
    }
    
    private static function IsScalarType($type) {
        switch(\strtolower($type)) {
            case "int":
            case "integer":
            case "bool":
            case "boolean":
            case "float":
            case "double":
            case "real":
            case "string":
                return true;
            default: 
                return false;
        }
    }
    
    private function TryGetMap(\ReflectionProperty $destinationProperty, &$map) {
        $key = $destinationProperty->class . '::' . $destinationProperty->name;
        if(\array_key_exists($key, $this->maps)) {
            $map = $this->maps[$key];
            return true;
        }
        return false;
    }
    
    private static function TryGetObjectArrayClass($type, &$outType) {
        if(\substr($type, -2) === '[]') {
            $outType = \substr($type, 0, -2); 
            return true;
        }
        return false;
    }
    
    private static function TryGetReflectionProperty($propertyName, $sourceClass, &$outSourceReflectionProperty) {
        $sourceReflectionClass = new \ReflectionClass($sourceClass);
        foreach($sourceReflectionClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $sourceReflectionProperty) {
            if($sourceReflectionProperty->name == $propertyName) {
                $outSourceReflectionProperty = $sourceReflectionProperty;
                return true;
            }
        }
        return false;
    }
    
    private static function TryParseType(\ReflectionProperty $property, &$outType) {
        $matches = array();
        $result = \preg_match('/@var\\s*([^\\s]*)/i', $property->getDocComment(), $matches);
        $outType = $matches[1];
        return $result;
    }
    
    
}

class CountConverter implements ITypeConverter {
    public function Convert($source) {
        return \count($source);
    }
}

class ImplodeConverter implements ITypeConverter {
    private $delimiter;
    
    /**
     * Returns a new ImplodeConverter instance. If specified, the $delimiter will be placed between every element of the source array.
     *
     * @param string $delimiter 
     */
    public function __construct($delimiter='') {
        $this->delimiter = $delimiter;
    }
    
    public function Convert($source) {
        return \implode($this->delimiter, $source);
    }
}


?>